using UnityEngine;
using UnityEngine.UIElements;
using MinesweeperPlus.Scripts.Abstractions.Enums;
using MinesweeperPlus.Scripts.Configs;
using MinesweeperPlus.Scripts.Utils;

namespace MinesweeperPlus.Scripts.UI
{
    public sealed class BottomPanel : UIElement
    {
        public new class UxmlFactory : UxmlFactory<BottomPanel> { }

        #region Constants
        private const int MIN_NUMBER = 0;
        private const int MAX_NUMBER = 999999999;
        #endregion

        #region Variables
        private IMGUIContainer _countIcon;
        private Label _countLabel;
        #endregion

        #region BottomPanel Methods
        public BottomPanel() : base() { }
        public BottomPanel(IconsConfig iconsConfig, StyleSheet styleSheet) : 
            base(iconsConfig, styleSheet) { }

        public void SetPanelType(BottomPanelType panelType) 
        {
            switch (panelType)
            {
                case BottomPanelType.Money:
                    SetPanelIcon(IconsConfig.MineIcon);
                    break;
                case BottomPanelType.Flags:
                    SetPanelIcon(IconsConfig.FlagIcon);
                    break;
            }
        }
        public void SetCountTextFromNumber(int number) =>
            _countLabel.text = Mathf.Clamp(number, MIN_NUMBER, MAX_NUMBER).ToString();
        #endregion

        #region Other Methods
        protected override void UpdateColorScheme()
        {
            base.UpdateColorScheme();

            Panel.SetBackgroundColor(ColorSchemeConfig.BackgroundColor);
            Panel.SetBorderColor(ColorSchemeConfig.BorderColor);

            _countIcon.SetBackgroundImageColor(ColorSchemeConfig.IconColor);
            _countLabel.SetTextColor(ColorSchemeConfig.TextColor);
        }

        protected override void CreatePanel()
        {
            base.CreatePanel();
            Panel.AddToClassList(Constants.UI_BOTTOM_PANEL_STYLE);

            _countIcon = CreateIMGUIContainer(
                IconsConfig.MineIcon,
                Constants.UI_BOTTOM_ELEMENT_STYLE);
            Panel.Add(_countIcon);

            _countLabel = new Label("0");
            _countLabel.AddToClassList(Constants.UI_BOTTOM_ELEMENT_STYLE);
            Panel.Add(_countLabel);
        }

        private void SetPanelIcon(VectorImage vectorImage)
        {
            _countIcon.SetBackgroundImage(vectorImage);
        }
        #endregion
    } 
}
